package reptile

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"robot/study/LiveRobot/define"
	"robot/study/LiveRobot/sdk/zlog"
)

//图片回复
type Image struct {
	U *define.Message2
}

//发送图片
func (ie *Image) Work() {
	var im define.Image
	var ty string
	var number int
	if ie.U.MessageType == "private" {
		ty = "private"
		number = int(ie.U.UserId)
	} else {
		ty = "group"
		number = int(ie.U.GroupId)
	}
	resp, err := http.Get("https://api.lolicon.app/setu/v2?r18=0")
	if err != nil {
		zlog.Error("图片加载失败", err)
	}
	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {

		}
	}(resp.Body)
	reader := bufio.NewReader(resp.Body)
	body, err2 := ioutil.ReadAll(reader)
	if err2 != nil {
		zlog.Error("读取失败")
	}
	err = json.Unmarshal(body, &im)
	if err != nil {
		return
	}
	fmt.Println(string(body))
	mes := &define.Msg{
		Msg_type: ty,
		Number:   number,
		Msg:      "[CQ:image,file=" + im.Data[0].Urls.Original + "]",
	}
	Sendmsg(mes)
}
